#include "StdAfx.h"
#include "DvMain.h"
#include <opencv2\opencv.hpp>
#include "../Dv/DvImage.h"
#include "../Dv/DvProcess.h"

using namespace std;
using namespace cv;

DvMain::DvMain(void)
{
	ProgromBegin();
}


DvMain::~DvMain(void)
{
}

DvMain* DvMain::InitProgrom()
{
	DvMain* progromMain = new DvMain();
	if(progromMain)
	{
		return progromMain;
	}
	return NULL;
}

void DvCallBack(int event, int x, int y, int flag, void* param);

void DvMain::ProgromBegin()
{

	CvCapture* pCapture = cvCaptureFromCAM(CV_CAP_ANY);
	cvSetCaptureProperty(pCapture,CV_CAP_PROP_FRAME_WIDTH,200);
	cvSetCaptureProperty(pCapture,CV_CAP_PROP_FRAME_HEIGHT,300);
	IplImage* pFame = cvQueryFrame(pCapture);
	IplImage* tempImg = cvCreateImage(cvGetSize(pFame), IPL_DEPTH_8U, 3);
	//cvNamedWindow("window1");
	cvNamedWindow("window2");
	cout<<"begin!"<<endl;
	DvImage* myImg = DvImage::initWithImage(pFame);
	while( pFame = cvQueryFrame(pCapture))
	{
		myImg->setImage(pFame);
		CvPoint newPos = cvPoint(0,0);
		for(int y = 0; y < myImg->getSize().height; y ++)
		{
			for(int x = 0; x < myImg->getSize().width; x ++)
			{
				newPos = cvPoint(x,y);
				if(DvCheckPointIsSkin(myImg->getImage(),newPos))
				{
					myImg->setColor(newPos, cvScalar(255,255,255));
				}
				else if(myImg->getGray(newPos)<30)
				{
					myImg->setColor(newPos, cvScalar(255,0,0));
				}
				else 
				{
					myImg->setColor(newPos, cvScalar(0,0,0));
				}

			}
		}

		cvShowImage("window2",pFame);
		cvSetMouseCallback("window2",DvCallBack);

		char c=cvWaitKey(33);  
		if(c==27)break; 
	}
	delete myImg;
	//cvReleaseImage(&pFame);
	//cvReleaseImage(&tempImg);
	cvReleaseCapture(&pCapture);
	cvDestroyWindow("window1");
	cvDestroyWindow("window2");

}


void DvCallBack(int event, int x, int y, int flag, void* param)
{
	if(event == CV_EVENT_LBUTTONDOWN)
		cout<<"x = "<<x<<" y = "<<y<<endl;
}